package com.ihelp.rest.mis.platform.service;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ihelp.rest.mis.platform.dao.UserTimeDao;
import com.ihelp.rest.mis.pojo.SysUser;
import com.ihelp.rest.mis.pojo.UserTime;
import com.ihelp.rest.utils.DateUtil;
import com.ihelp.rest.utils.JsonCommon;
import com.ihelp.rest.utils.ReadExcelUtil;

@Service
public class UserTimeService {

	@Autowired
	private UserTimeDao dao;
	
	public String findUsertime(int start, int pageSize) {
//		return JsonCommon.getGridJson(this.dao.countAll(),
//				this.dao.listAll(start, pageSize)).toString();
		return JsonCommon.getGridJson(this.dao.getAllRC(start, pageSize)).toString();
		
	}
	
	public String saveUserTime(FileInputStream input, SysUser user){
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("success", false);
		result.put("data", "");
		
		String time = DateUtil.date2String(new Date());
		ReadExcelUtil readExcel = new ReadExcelUtil(input); 
		try {
			readExcel.open();
		} catch (IOException e) {
			e.printStackTrace();
			result.put("data", e.getMessage());
		}
		readExcel.setSheetNum(0);
		int count = readExcel.getRowCount();
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		Date browseDate = null;
		String[] coldate = readExcel.readExcelLine(0);
		try{
			browseDate = formatDate.parse(coldate[0]); 
		}catch(java.text.ParseException e){
			Calendar  cal=Calendar.getInstance();
			cal.set(Calendar.DATE, 1);
			cal.add(Calendar.DATE, -15);	
			browseDate = cal.getTime();
		}		
		//List<UserTime> timeList = new ArrayList<UserTime>();
		//起始行下标
		int indexRow = 3;
		//计算机名
		String computerName = null;
		try{
			for(int i = indexRow; i < count; i++){
				String[] columns = readExcel.readExcelLine(i);
				int columnsLen = columns.length;
				String columnStr = Arrays.toString(columns);
				if(columnsLen == 2){
					if(StringUtils.isNotBlank(columns[0])){
						//代表计算机名称
						computerName = columns[0];
					}
					continue;
				}
				else if(columnStr.contains("序号")){
					continue;
				}
				else{
					UserTime userTime = new UserTime();
					userTime.setAddTime(time);
					userTime.setAddUser(user);
					userTime.setUrl(columns[1]);
					userTime.setBrowseTime(columns[2]);
					userTime.setRercent(columns[3]);
					userTime.setBrowseDate(formatDate.format(browseDate));
					userTime.setComputerName(computerName);
					//timeList.add(userTime);
					dao.save(userTime);
				}
			}
			result.put("success", true);
			result.put("data", "导入成功");
		}catch(Exception e){
			result.put("data", e.getMessage());
		}
		return JSONObject.fromObject(result).toString();
	}
	
	
	
}
